package org.wlgzs.shop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.wlgzs.shop.bean.*;
import org.wlgzs.shop.service.IOrderTransportDateService;
import org.wlgzs.shop.service.ManagerService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


@Api(tags = "这是管理者的接口", value = "ManagerController")
@Controller
@RequestMapping("/administrator")
public class ManagerController {
    @Resource(name = "managerService")
    ManagerService managerService;

    @Autowired
    IOrderTransportDateService orderTransportDateService;

    Logger logger=LoggerFactory.getLogger(ManagerController.class);

    @RequestMapping("/addManager")
    @ResponseBody
    @ApiOperation(value = "添加管理者",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query", name = "Manager", value = "管理者类", required = true, dataType = "UserDate")
    public Msg addManager(UserDate Manager) {
        return managerService.addManager(Manager);
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public Msg addUser(UserDate date){
        date.setIntegral(0);
        date.setMoney(0f);
        date.setPassword("123456");
        System.out.println(date);
        if(managerService.addUser(date)){
         return Msg.success().add("win","添加成功");
        }
        else {
            return Msg.fail().add("errors","添加失败");
        }
    }

    @RequestMapping("/deleteUser")
    @ApiOperation(value = "删除用户",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", required = true, dataType = "int")
    public String delectManager(int userId){

        managerService.deleteUser(userId);
        return "redirect:administrator/backstageUserJump";
    }


    @ApiOperation(value = "管理者登陆",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "account", value = "用户名", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping("/loginManager")
    public Msg loginManager(@Param("account") String account, @Param("password") String password, HttpSession session) {
        return managerService.loginManager(account, password, session);
    }


    /**
     * @return ModelAndView
     * <p>
     * 修改管理员密码
     */
    @ApiOperation(value = "修改管理者密码",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query", name = "password", value = "修改后的密码", required = true, dataType = "string")
    @ResponseBody
    @RequestMapping("/setManagerPass")
    public Msg setManagerPass(UserDate date, HttpSession session) {
        return managerService.setManagerPass(date, session);
    }

    /**
     * @return ModelAndView
     * <p>
     * 查询所有商品信息
     */
    @RequestMapping("/selectAllGoods")
    @ResponseBody
    @ApiOperation(value = "查询所有商品信息",httpMethod = "POST", response =ModelAndView.class)
    @ApiImplicitParam(paramType = "query", name = "page", value = "页码，默认为一", required = true, dataType = "int")
    public ModelAndView selectGoodsDate(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstage_business/business_index");
        PageHelper.startPage(page, 10);
        List<GoodsData> list = managerService.selectAllGoodsDate();
        PageInfo pageInfo = new PageInfo(list, 15);
        modelAndView.addObject("list", pageInfo);
        return modelAndView;
    }


    /**
     * @return ModelAndView
     * <p>
     * 查询用户信息
     */
    @RequestMapping("/selectUser")
    @ResponseBody
    @ApiOperation(value = "模糊查询用户",httpMethod = "POST", response =ModelAndView.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", value = "页码，默认为一", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "userDate", value = "模糊查询的信息", required = true, dataType = "String")
    })
    public ModelAndView selectUserDate(@Param("userDate") String userDate, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page, 10);
        List<UserDate> list = managerService.selectUser(userDate);
        PageInfo pageInfo = new PageInfo(list, 15);
        modelAndView.setViewName("backstageManagementSystem/backstage_user");
        modelAndView.addObject("list", pageInfo);
        return modelAndView;
    }


    /**
     * @return ModelAndView
     * <p>
     * 查看所有订单信息
     */
    @RequestMapping("/selectAllOrder")
    @ResponseBody
    @ApiOperation(value = "查看所有订单信息",httpMethod = "POST", response =ModelAndView.class)
    @ApiImplicitParam(paramType = "query", name = "page", value = "页码，默认为一", required = true, dataType = "int")
    public ModelAndView selectAllOrderDate(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page, 10);
        List<OrderTransportData> list = managerService.selectAllOrderDate();
        PageInfo pageInfo = new PageInfo(list, 15);
        modelAndView.setViewName("backstageManagementSystem/backstage_point");
        modelAndView.addObject("list", pageInfo);
        return modelAndView;
    }


    /**
     * @return Msg
     * <p>
     * 修改商品信息
     */
    @ApiOperation(value = "修改商品信息",httpMethod = "POST", response =Msg.class)
    @RequestMapping("/setGoods")
    @ResponseBody
    public Msg setGoodsDate(GoodsData data) {
        return managerService.setGoodsDate(data);
    }

    /**
     * @return Msg
     * <p>
     * 修改用户信息
     */
    @ApiOperation(value = "修改用户信息",httpMethod = "POST", response =Msg.class)
    @RequestMapping("/setUser")
    @ResponseBody
    public Msg setUserDate(UserDate date) {
        return managerService.setUserDate(date);
    }


    /**
     * @return Msg
     * <p>
     * 修改订单信息
     */
    @ApiOperation(value = "修改订单信息",httpMethod = "POST", response =Msg.class)
    @RequestMapping("/setOrder")
    @ResponseBody
    public Msg setOrderDate(OrderTransportData data) {
        return managerService.setOrderDate(data);
    }


    @RequestMapping("/allUser")
    @ResponseBody
    @ApiOperation(value = "所用用户", notes = "有分页",httpMethod = "POST", response =ModelAndView.class)
    @ApiImplicitParam(paramType = "query", name = "pn", value = "页码，默认为一", required = true, dataType = "int")
    public ModelAndView getAllUser(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(pn, 10);
        List<UserDate> list = managerService.selectAllUserDate();
        PageInfo pageInfo = new PageInfo(list, 15);
        modelAndView.setViewName("backstageManagementSystem/backstage_user");
        modelAndView.addObject("allUser", pageInfo);
        return modelAndView;
    }

    /**
     * @return
     */
    @RequestMapping("/selectOrder")
    @ApiOperation(value = "模糊查询订单",httpMethod = "POST", response =ModelAndView.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码，默认为一", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "date", value = "模糊查询信息", required = true, dataType = "string")
    })
    public ModelAndView getOrder(@RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo, String date) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(pageNo, 10);
        List<Store> list = managerService.selectAllStoreLikeDate(date);
        PageInfo pageInfo = new PageInfo(list, 15);
        modelAndView.setViewName("backstageManagementSystem/backstage_point");
        modelAndView.addObject("list", list);
        return modelAndView;
    }

    /**
     * @param pageNo
     * @return 模糊获取商品信息
     */
    @RequestMapping("/selectGoods")
    @ApiOperation(value = "模糊获取商品信息",httpMethod = "POST", response =ModelAndView.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码，默认为一", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "date", value = "模糊查询信息", required = true, dataType = "string")
    })
    public ModelAndView getAllGoods(@RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo, String date) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(pageNo, 10);
        List<GoodsData> list = managerService.selectAllGoodsLikeDate(date);
        PageInfo pageInfo = new PageInfo(list, 15);
        modelAndView.setViewName("backstageManagementSystem/backstage_commodity");
        modelAndView.addObject("list", pageInfo);
        return modelAndView;
    }

    //添加一级分类

    //添加二级分类




    /**
     * @return
     */
    @ApiOperation(value = "跳转'backstage_index'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/backstageIndexJump")
    public ModelAndView backstageIndexJump() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstageManagementSystem/backstage_index");
        return modelAndView;
    }

    /**
     * @return
     */
    @ApiOperation(value = "跳转'backstage_addcommodity'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/backstageAddcommodityJump")
    public ModelAndView backstageAddcommodityJump(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,10);
        List<OrderTransportData> list=orderTransportDateService.allOrder();
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("order",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_addcommodity");
        return modelAndView;
    }

    /**
     * @return
     */
    @ApiOperation(value = "跳转'backstage_commodity'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/backstageCommodityJump")
    public ModelAndView backstageCommodityJump(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,15);
        List<GoodsData> list= managerService.selectAllGoodsDate();
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("goods",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_commodity");
        return modelAndView;
    }

    /**
     * @return
     */
    @ApiOperation(value = "跳转'backstage_login'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/backstageLoginJump")
    public ModelAndView backstageLoginJump() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstageManagementSystem/backstage_login");
        return modelAndView;
    }

    /**
     * @return
     */
    @ApiOperation(value = "跳转'backstage_logon'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/backstageLogonJump")
    public ModelAndView backstageLogonJump() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstageManagementSystem/backstage_logon");
        return modelAndView;
    }



    /**
     * @return
     */
    @ApiOperation(value = "跳转'backstage_point'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/backstagePointJump")
    public ModelAndView backstagePointJump(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,15);
        List<UserDate> list=managerService.selectAllUserDate();
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("user",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_point");
        return modelAndView;
    }



    /**
     * @return
     */
    @ApiOperation(value = "跳转'backstage_shop'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/backstageShopJump")
    public ModelAndView backstageShopJump(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,15);
        List<Store> list=managerService.selectAllStore();
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("store",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_shop");
        return modelAndView;
    }



    /**
     * @return
     */
    @ApiOperation(value = "跳转'backstage_user'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/backstageUserJump")
    public ModelAndView backstageUserJump(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,15);
        List<UserDate> list=managerService.selectAllUserDate();
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("user",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_user");
        return modelAndView;
    }


    @ApiOperation(value = "跳转'business_index'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/business_index")
    public ModelAndView business_indexJump(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstage_business/business_index");
        return modelAndView;
    }

    @ApiOperation(value = "跳转'business_personal'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/business_personal")
    public ModelAndView business_personalJump() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstage_business/business_personal");
        return modelAndView;
    }

    @ApiOperation(value = "跳转'store_logon'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/store_logon")
    public ModelAndView store_logonJump() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstage_business/store_logon");
        return modelAndView;
    }

    @ApiOperation(value = "跳转'store_logon'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/jumpBackstageComment")
    public String jumpBusinessContent(){
        return "backstageManagementSystem/backstage_comment";
    }


    @ApiOperation(value = "用户搜索",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/selectUserLikeDate")
    public ModelAndView seletDate(@Param("date") String date,@RequestParam(value = "page", defaultValue = "1") Integer page){
        ModelAndView modelAndView = new ModelAndView();
        if(date==null||date.equals("")){
            modelAndView.setViewName("redirect:/administrator/backstageUserJump");
            return modelAndView;
        }
        PageHelper.startPage(page,15);
        List<UserDate> list=managerService.selectAllLikeDate(date);
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("selectUser",date);
        modelAndView.addObject("user",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_user");
        return modelAndView;
    }

    @ApiOperation(value = "用户搜索",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/selectNumberUserLikeDate")
    public ModelAndView seletNumberDate(@Param("date") String date,@RequestParam(value = "page", defaultValue = "1") Integer page){

        ModelAndView modelAndView = new ModelAndView();
        if(date==null||date.equals("")){
            modelAndView.setViewName("redirect:/administrator/backstageUserJump");
            return modelAndView;
        }
        PageHelper.startPage(page,15);
        List<UserDate> list=managerService.selectAllLikeDate(date);
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("selectUser",date);
        modelAndView.addObject("user",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_user");
        return modelAndView;
    }





    @ApiOperation(value = "店铺搜索",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/selectStoreLikeDate")
    public ModelAndView seletStoreDate(@Param("date") String date,@RequestParam(value = "page", defaultValue = "1") Integer page){
        ModelAndView modelAndView = new ModelAndView();
        if(date==null||date.equals("")){
            modelAndView.setViewName("redirect:/administrator/backstageShopJump");
            return modelAndView;
        }
        PageHelper.startPage(page,15);
        List<Store> list=managerService.selectAllStoreLikeDate(date);
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("selectStore",date);
        modelAndView.addObject("store",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_shop");
        return modelAndView;
    }

    @ApiOperation(value = "商品搜索",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/selectGoodsLikeDate")
    public ModelAndView seletGoodsDate(@Param("date") String date,@RequestParam(value = "page", defaultValue = "1") Integer page){
        System.out.println(date);
        ModelAndView modelAndView = new ModelAndView();
        if(date==null||date.equals("")){
            modelAndView.setViewName("redirect:/administrator/backstageCommodityJump");
            return modelAndView;
        }
        PageHelper.startPage(page,15);
        List<GoodsData> list=managerService.selectAllGoodsLikeDate(date);
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("selectGoods",date);
        modelAndView.addObject("goods",pageInfo);
        modelAndView.setViewName("redirect:backstageManagementSystem/backstage_commodity");
        return modelAndView;
    }

    @ApiOperation(value = "订单搜索",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/selectOrderLikeDate")
    public ModelAndView seletOrderDate(@Param("date") String date,@RequestParam(value = "page", defaultValue = "1") Integer page){
        System.out.println("order:"+date);
        ModelAndView modelAndView = new ModelAndView();
        if(date==null||date.equals("")){
            modelAndView.setViewName("redirect:/administrator/backstageAddcommodityJump");
            return modelAndView;
        }
        PageHelper.startPage(page,15);
        List<OrderTransportData> list= orderTransportDateService.selectOrder(date);
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("selectOrder",date);
        modelAndView.addObject("order",pageInfo);
        modelAndView.setViewName("backstageManagementSystem/backstage_addcommodity");
        return modelAndView;
    }

    @ApiOperation(value = "添加店铺",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/addShop")
    @ResponseBody
    public Msg addShop(@Param("storeName")String storeName,@Param("userId")String userId,@Param("storeKind")String storeKind){
        System.out.println(storeName+"=="+storeKind+"=="+userId);
        return managerService.addShop(storeName,userId,storeKind);
    }


    @RequestMapping("/upUserDate")
    @ResponseBody
    public Msg upUser(@Param("userId")int userId,@Param("userName")String userName,@Param("post")String post,@Param("phone")String phone){
        UserDate userDate=new UserDate();
        userDate.setUserId(userId);
        if(!userName.equals("")){
            userDate.setUserName(userName);
        }
        if(!post.equals("")){
            userDate.setPost(post);
        }
        if(!phone.equals("")){
            userDate.setPhone(phone);
        }
        logger.info(userDate.toString());
        return managerService.upUser(userDate);
    }

    @RequestMapping("/upShopDate")
    @ResponseBody
    public Msg upShop(@Param("storeId")int storeId,@Param("storeName")String storeName){
        Store store=new Store();
        store.setStoreId(storeId);
        if(!storeName.equals("")){
            store.setStoreName(storeName);
        }
        logger.info(store.toString());
        return managerService.upShop(store);
    }

    @RequestMapping("/delectShopDate")
    @ResponseBody
    public Msg delectShop(int id){
        System.out.println(id);
        return managerService.delectShop(id);
    }


    @RequestMapping("/upGoodsDate")
    @ResponseBody
    public Msg upGoods(@Param("id")int id,@Param("goodsPrice")String goodsPrice,@Param("goodsName")String goodsName){
        GoodsData goodsData=new GoodsData();
        goodsData.setId(id);
        if(!goodsName.equals("")){
            goodsData.setGoodsName(goodsName);
        }
        if(!goodsPrice.equals("")){
            goodsData.setGoodsPrice(Float.parseFloat(goodsPrice));
        }
        logger.info(goodsData.toString());
        return managerService.upGoods(goodsData);
    }

}
